package khmt.lvtn.dhbk.process;

import khmt.lvtn.dhbk.gui.Gui;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import khmt.lvtn.dhbk.summary.RetrieveOntology;

import khmt.lvtn.dhbk.model.SentimentModel;
import khmt.lvtn.dhbk.model.process.MainProcess;
import khmt.lvtn.dhbk.model.process.PR;
import khmt.lvtn.dhbk.comment.Comment;


public class Run {
	static private String specialCasePath = "data/special.txt";
	public static String adjPath = "data/tinhtu.txt";
	public static String verbPath = "data/dongtu.txt";
	public static String entityPath = "data/thucthe.txt";
	public static String propertyPath = "data/thuoctinh.txt";
	public static String auxiPath = "data/trotu.txt";
	public static String negativePath = "data/phudinh.txt";
	public static String pronoun = "data/daitu.txt";
	
	static private String startEntiTag = "<enti>";
	static private String endEntiTag = "</enti>";
	static private String startAdjTag = "<adj>";
	static private String endAdjTag = "</adj>";
	
	static public String commentsPath = "input/comments.txt";
	static public String resultForCommentsPath = "result/resultForAllComments.txt";
	static public String PositiveOrNegativeResultForCommentsPath = "positiveOrNegativeResult/resultForAllComments.txt";
	static public String realResult = "input/plain.txt";
	
	static public float p = 100;
	static public float r = 100;
	
	public static int realPos = 0, realNeg = 0, realPN = 0, sysPos = 0, sysNeg = 0, sysPN = 0;
	
	public Run() {
		// TODO Auto-generated constructor stub
		setSpecialCasePath("data/specialEntityAdj.txt");
	}
	public static void setSpecialCasePath(String specialCasePath) {
		Run.specialCasePath = specialCasePath;
	}
	public static String getSpecialCasePath() {
		return specialCasePath;
	}
	public static void setStartEntiTag(String startEntiTag) {
		Run.startEntiTag = startEntiTag;
	}
	public static String getStartEntiTag() {
		return startEntiTag;
	}
	public static void setEndEntiTag(String endEntiTag) {
		Run.endEntiTag = endEntiTag;
	}
	public static String getEndEntiTag() {
		return endEntiTag;
	}
	
	public static void setNegativePath(String negativePath) {
		Run.negativePath = negativePath;
	}
	public static String getNegativePath() {
		return negativePath;
	}
	public static void setPropertyPath(String propertyPath) {
		Run.propertyPath = propertyPath;
	}
	public static String getPropertyPath() {
		return propertyPath;
	}
	public static void setEntityPath(String entityPath) {
		Run.entityPath = entityPath;
	}
	public static String getEntityPath() {
		return entityPath;
	}
	public static void setVerbPath(String verbPath) {
		Run.verbPath = verbPath;
	}
	public static String getVerbPath() {
		return verbPath;
	}
	public static void setAdjPath(String adjPath) {
		Run.adjPath = adjPath;
	}
	public static String getAdjPath() {
		return adjPath;
	}
	public static void setStartAdjTag(String startAdjTag) {
		Run.startAdjTag = startAdjTag;
	}
	
	public static void setEndAdjTag(String endAdjTag) {
		Run.endAdjTag = endAdjTag;
	}
	public static String getEndAdjTag() {
		return endAdjTag;
	}
	public static String getStartAdjTag() {
		// TODO Auto-generated method stub
		return startAdjTag;
	}
	
	public static long processForCommentsFile(String inputFilePath, String outputFilePath, String outputFilePath1){
		long time = System.currentTimeMillis();
		try {
			RetrieveOntology retrOntology = RetrieveOntology.getInstant();
			retrOntology.readOntology(Run.entityPath, Run.propertyPath, Run.adjPath, Run.verbPath, Run.auxiPath, Run.negativePath, Run.pronoun);
			FileReader fr = new FileReader(inputFilePath);
			BufferedReader br = new BufferedReader(fr);
			String line = null;
			String result = "";
			String result1 = "";
			BufferedWriter out = new BufferedWriter(new FileWriter(Run.resultForCommentsPath));
			BufferedWriter out1 = new BufferedWriter(new FileWriter(outputFilePath1));
			BufferedReader br1 = new BufferedReader(new FileReader(realResult));
			int lineNumber = 0;
			
			int numberOfMultiEnityComment = 0;
			
			while((line = br.readLine())!=null){
				lineNumber++;
				Comment comment = new Comment(line);
				if(comment.getResult().getOneOrMultiEntity() == 1){
					
					String Entity = "T";
					String p = "p:";
					String n = "n:";
					String pn = "pn:";
					if(comment.getSuperEntiTextList().size() == 1){
						Entity = comment.getSuperEntiTextList().get(0);
						if(Entity.equals("")||Entity.equals(null)){
							Entity = "T";
						}
					}
					result = Float.toString(comment.getResult().getSentimentForOneCase());
					if(comment.getResult().getSentimentForOneCase()>0){
						result1 = "p";
						sysPos ++;
					}
					else if(comment.getResult().getSentimentForOneCase()<0){
						result1 = "n";
						sysNeg ++;
					}
					else{
						result1 = "pn";
						sysPN ++;
					}
					
					String tem = result1;
					if(tem.equals("p")){
						p += Entity;
					}
					else if(tem.equals("n")){
						n += Entity;
					}
					else {
						pn += Entity;
					}
				
					result1 = p+";"+n+";"+pn;
				}
				else{
					ArrayList<SentimentModel> sentiList = comment.getResult().getSentimentForMultCase();
					numberOfMultiEnityComment ++;
					result = "";
					result1 = "";
					String p = "p:";
					String n = "n:";
					String pn = "pn:";
					for(int i=0; i<sentiList.size(); i++){
						if(i ==0 ){
							result = "";
						}
						result += sentiList.get(i).getWordSentiment()+"-"+Float.toString(sentiList.get(i).getScore())+" ";
						if(sentiList.get(i).getScore()>0){
							if(p.equals("p:")){
								p += sentiList.get(i).getWordSentiment();
							}
							else{
								p += ","+sentiList.get(i).getWordSentiment();
							}
							
						}
						else if(sentiList.get(i).getScore()<0){
							
							if(n.equals("n:")){
								n += sentiList.get(i).getWordSentiment();	
							}
							else{
								n += ","+sentiList.get(i).getWordSentiment();
							}
						}
						else{
							if(pn.equals("pn:")){
								pn += sentiList.get(i).getWordSentiment();	
							}
							else{
								pn += ","+sentiList.get(i).getWordSentiment();
							}
							
						}
					}	
					result1 = p+";"+n+";"+pn;
				}
				
				
				out.write(result+"\n");
				out1.write(result1+"\n");
				String line1 = br1.readLine();
				if(line1 != null){
					line1 = line1.replaceAll(" ", "");
				}
				else{
					line1 = "";
				}
				
				if(line1.compareTo("p") == 0){
					realPos++;
				}
				else if(line1.compareTo("n") == 0){
					realNeg ++;
				}
				else{
					realPN ++;
				}
			}
			fr.close();
			out1.close();
			out.close();			
			br1.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
	//	processResultByTraining();
//		Run.deleteTempFile(); 
		Run.deleteTempFile();
		return System.currentTimeMillis()-time;		
	}
	
	static private void processResultByTraining(){
		final String trainningFilePath = "src/Trainning/Trainning.txt";
		final String focusPPath = "" +
				"TrainningResult/focusP.txt";
		final String focusRPath = "TrainningResult/focusR.txt";
		String resultFilePath = Run.resultForCommentsPath;
		float upperForP = 0;
		float underForP = 0;
		float upperForR = 0;
		float underForR = 0;
		
		// read Trainning.txt
		try {
			BufferedReader rfTrainning = new BufferedReader(new FileReader(trainningFilePath));
			String line = "";
			for(int i=0; i<6; i++){
				try {
					line = rfTrainning.readLine();
					if(line == null){
						break;
					}
					if(i==0 || i==3){
						continue;
					}
					else if(i==1){
						upperForP = Float.parseFloat(line);
					}
					else if(i==2){
						underForP = Float.parseFloat(line);
					}
					else if(i==4){
						upperForR = Float.parseFloat(line);
					}
					else if(i==5){
						underForR = Float.parseFloat(line);
						rfTrainning.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// done read trainning.txt
		
		try {
			BufferedReader rb = new BufferedReader(new FileReader(resultFilePath));
			try {
				BufferedWriter wbP = new BufferedWriter(new FileWriter(focusPPath));
				BufferedWriter wbR = new BufferedWriter(new FileWriter(focusRPath));
				String line = "";
				String writeForP = "";
				String writeForR = "";
				String pP = "p:";
				String pR = "p:";
				String nP = "n:";
				String nR = "n:";
				String pnP = "pn:";
				String pnR = "pn:";
				
				while((line = rb.readLine())!=null){
					StringTokenizer stk = new StringTokenizer(line, " ");
					String token = "";
					while(stk.hasMoreElements()){
						token = stk.nextToken();
						String entity = "";
						String stringForScore = "";
						float score = 0;
					
						if(token.contains("--")){
							StringTokenizer temp = new StringTokenizer(token, "--");
							entity = temp.nextToken();
							stringForScore = temp.nextToken();
							score = Float.parseFloat(stringForScore);
							score = -1*score;
							if(score<underForP){
								nP+=entity;
							}
							else{
								pnP+=entity;
							}
							if(score<underForR){
								nR+=entity;
							}
							else{
								pnR+=entity;
							}
						}
						else{
							StringTokenizer temp = new StringTokenizer(token, "-");
							entity = temp.nextToken();
							if(temp.countTokens()>=2){
								stringForScore = temp.nextToken();	
							}
							else{
								stringForScore = "0";
							}
							
							score = Float.parseFloat(stringForScore);
							if(score>upperForP){
								pP+=entity;
							}
							else{
								pnP+=entity;
							}
							if(score>upperForR){
								pR+=entity;
							}
							else{
								pnR+=entity;
							}
						}
						writeForP = pP+";"+nP+";"+pnP;
						writeForR = pR+";"+nR+";"+pnR;
						wbP.write(writeForP+"\n");
						wbR.write(writeForR+"\n");
					}
					pP = "p:"; nP = "n:"; pnP = "pn:";
					pR = "p:"; nR = "n:"; pnR = "pn:";
					
				
				}
				wbR.close();
				wbP.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		MainProcess mainProcess = new MainProcess();
		PR focusPMax = mainProcess.getPR(Run.realResult, Run.commentsPath);
		PR focusRMax = mainProcess.getPR(Run.realResult, Run.commentsPath);
		String pathResult = "TrainningResult/result.txt";
		try {
			BufferedWriter wb = new BufferedWriter(new FileWriter(pathResult));
			String forWrite = "focusP:"+"\n"+
								"    "+"P:"+focusPMax.getP()+"\n"+
								"    "+"R:"+focusRMax.getR()+"\n"+
							  "focusR:"+"\n"+
							  	"    "+"P:"+focusRMax.getP()+"\n"+
							  	"    "+"R:"+focusRMax.getR();
			wb.write(forWrite);
			wb.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void deleteTempFile(){
		File adjFile = new File(Run.adjPath);
		File verbFile = new File(Run.verbPath);
		File entityFile = new File(Run.entityPath);
		File propertyFile = new File(Run.propertyPath);
		File auxFile = new File(Run.auxiPath);
		File negFile = new File(Run.negativePath);
		File proFile = new File(Run.negativePath);
		adjFile.delete();
		verbFile.delete();
		entityFile.delete();
		propertyFile.delete();
		auxFile.delete();
		negFile.delete();
		proFile.delete();
	}
	
	public static void  main(String []args){
		RetrieveOntology retrOntology = RetrieveOntology.getInstant();
		retrOntology.readOntology(Run.entityPath, Run.propertyPath, Run.adjPath, Run.verbPath, Run.auxiPath, Run.negativePath, Run.pronoun);
		System.out.println("Running....");
		new Gui();
	}
}